<script setup>
import {ref} from 'vue'
import pageofficeCtrlUtil from "@/plugins/pageoffice/pageoffice-word-excel.js";
import PageOfficeFile from "@/components/PageOfficeFile/index.vue";
import {getDocProcessApprovalFileInfo} from "@/api/document/approvalFile.js";
import useLoginUserStore from "@/store/modules/user.js";
import AdviceAdd from "@/views/document/common/advice/adviceAdd.vue";
import {approvalFunctions, approvalAdviceFunctions} from "@/utils/document-process.js";

const isEdit = ref(false)
const poHtml = ref('')
let docId = undefined
const loginUserStore = useLoginUserStore()
const data = reactive({
  adviceDialog: {
    isOpen: false,
    title: '呈批表--审核意见'
  },
  advice: {
    message: '',
    initOperateUserName: '[审核人员]',
    initOperateDate: '[审核日期]',
    initMessage: '[审核意见]'
  },
  //当前环节呈批表中的数据区域名称（也叫书签名称），这些数据区域是在公文模板中提前添加好的。
  //此开源项目中，直接在前端组件中写为固定值。实际开发项目时，开发人员可根据实际开发需求自行调整此处代码
  adviceDataRegion: {
    dataRegionNameForAdvice: 'PO_auditAdvice',
    dataRegionNameForOperateUserName: 'PO_auditUser',
    dataRegionNameForOperateDate: 'PO_auditDate'
  }
})
const {adviceDialog, advice, adviceDataRegion} = toRefs(data)
const { handleAdviceDialogOpen, handleCloseAdvice, handleSubmitAdvice, handleDeleteAdvice}
    = approvalAdviceFunctions(adviceDialog.value, advice.value, adviceDataRegion.value)
const { saveApprovalFile } = approvalFunctions()

//组装控件所需的自定义按钮的参数,
// 数组的第一个元素： 按钮的标题（如果设置为 "-"，表示此按钮是分隔线）；第二个元素： 点击当前按钮时触发的 JavaScript 函数名；第一个元素： 按钮图标的索引；
//组装PageOfficeCtrl控件所需的自定义按钮参数
const customButtonsForPO = [
  ["保存", "save", 1],
  ["另存", "saveAsFile", 11],
  ["添加审核意见", "addAdvice", 10],
  ["清除审核意见", "deleteAdvice", 14],
  ["-", "", 0]
]

onMounted(() => {
  //获取pageoffice客户端传过来的参数
  docId = JSON.parse(pageofficectrl.WindowParams)
  getFile()
})

function getFile() {
  getDocProcessApprovalFileInfo(docId).then(result => {
    poHtml.value = result.html
    isEdit.value = result.isEdit
    //挂载PageOfficeCtrl控件需要调用的函数，例如控件触发的事件、自定义按钮触发的函数等。
    if (!result.isPdf) {
      pageofficeCtrlUtil.jsFunctionMounted(customJSFunctions, customButtonsForPO)
    }
  })
}

const customJSFunctions = {
  //控件初始化事件，函数名是指定的，不可修改，此处根据实际情况重写OnPageOfficeCtrlInit事件代码
  OnPageOfficeCtrlInit: () => {
    // 添加自定义按钮
    pageofficeCtrlUtil.addCustomButtons(isEdit.value ? customButtonsForPO : null)
  },
  AfterDocumentOpened: () => {
    const adviceMessage = pageofficectrl.word.GetValueFromDataRegion(adviceDataRegion.value.dataRegionNameForAdvice)
    advice.value.message = adviceMessage === advice.value.initMessage ? '' : adviceMessage
  },
  //部分自定义按钮执行的js函数
  addAdvice: () => {
    handleAdviceDialogOpen()
  },
  deleteAdvice: () => {
    handleDeleteAdvice()
  },
  save: () => {
    saveApprovalFile(docId, advice.value.message)
  }
}

</script>

<template>
  <AdviceAdd :title="adviceDialog.title" :message="advice.message" :isOpen="adviceDialog.isOpen" :isFromApprovalFile="true"
          @handleSubmitAdvice="handleSubmitAdvice"
          @close="handleCloseAdvice"/>
  <PageOfficeFile :poHtml="poHtml"/>
</template>

